package com.itwxz.system.controller.common;

import com.itwxz.common.constant.CacheConstants;
import com.itwxz.common.core.domain.R;
import com.itwxz.common.core.redis.RedisCache;
import com.itwxz.system.domain.po.Weather;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/weather")
@RequiredArgsConstructor
public class WeatherController {
	private final RedisCache redisCache;
	private static final String WEATHER_KEY = CacheConstants.WEATHER_KEY;

	/**
	 * 获取最新天气（从 Redis 取）
	 */
	@GetMapping
	public R<Weather> getWeather() {
		Weather weather = redisCache.getCacheObject(WEATHER_KEY);
		if (weather == null) {
			log.warn("Redis 中暂无天气数据");
			return R.fail("暂无天气数据，请稍后再试");
		}
		return R.ok(weather);
	}
}
